<?php

namespace App\Http\Middleware;

use Closure;

class AllowCrossRequest
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response           = $next($request);

        // 跨域
        /*$host_name = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';
        $request
            ->header('Content-Type: text/html;charset=utf-8');
        $request
            ->header("Access-Control-Allow-Origin: ".$host_name);
        $request
            ->header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE');
        $request
            ->header("Access-Control-Allow-Credentials: true");
        $request
            ->header("Access-Control-Allow-Headers:Token,Uid,Terminal,Content-Type,Referer,User-Agent,Content-Type,Sec-Fetch-Mode,Content-Length,Accept-Encoding,X-Requested-with, Origin, XFILENAME,XFILECATEGORY,XFILESIZE");*/

        /*$response
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*')
            ->header('Access-Control-Request-Headers', '*');*/



       //header('Access-Control-Allow-Origin', '*');
        /*$response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept');
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS');*/
        //$response->header('Access-Control-Allow-Credentials', 'true');



        /*$IlluminateResponse = 'Illuminate\Http\Response';
        $SymfonyResopnse    = 'Symfony\Component\HttpFoundation\Response';
        $headers            = [
            'Access-Control-Allow-Origin'  => '*',
            'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, PATCH, DELETE',
            'Access-Control-Allow-Headers' => 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers, X-CSRF-TOKEN'
        ];

        echo time();


        if ($response instanceof $IlluminateResponse) {
            foreach ($headers as $key => $value) {
                $response->header($key, $value);
            }
            return $response;
        }
        if ($response instanceof $SymfonyResopnse) {
            foreach ($headers as $key => $value) {
                $response->headers->set($key, $value);
            }
            return $response;
        }*/


        return $response;
    }
}


